const config = require('../../../config/config.local');
const amazonMws = require('amazon-mws')(config.AWSAccessKeyId, config.AWSSecretKey);
const isProduction = process.env.NODE_ENV === 'production';
const jsonflatten = require('pt-core-common').util.jsonflatten;

const createShipment = {};
createShipment.generateOption = function(seller_id, mws_auth_token, shippingServiceId, shipmentRequestDetails, shippingServiceOfferId=null, hazmatType=null, labelFormatOption=null, shipmentLevelSellerInputsList=null){
  let baseOption = {
    'Version': '2015-06-01',
    'Action': 'CreateShipment',
    'SellerId': seller_id,
    'MWSAuthToken': mws_auth_token,
    'ShippingServiceId': shippingServiceId,
  };

  if (shippingServiceOfferId)
    baseOption['ShippingServiceOfferId'] = shippingServiceOfferId;

  const option = Object.assign(baseOption,
    jsonflatten.flatten(shipmentRequestDetails, null, baseIndex=1),
    jsonflatten.flatten(hazmatType, null, baseIndex=1),
    jsonflatten.flatten(labelFormatOption, null, baseIndex=1),
    jsonflatten.flatten(shipmentLevelSellerInputsList, null, baseIndex=1));
  return option;
};

createShipment._ = function(options) {
  if (!isProduction) {
    return amazonMws.merchantFulfillment.create(options);
  } else {
    console.error("bot not implemented!")
  }
};

createShipment.createShipment = function(seller_id, mws_auth_token, shipmentRequestDetails) {
  return this._(this.generateOption(seller_id, mws_auth_token, shipmentRequestDetails));
};

module.exports = createShipment;